/*
 * @Author: HuangYue
 * @Date: 2022-04-12 15:37:06
 * @LastEditors: HuangYue
 * @LastEditTime: 2022-04-28 00:56:05
 * @FilePath: /graduation/mock/hospitalManage.js
 * @Description:
 */
import { randomName, randomID, randomPhoneNumber, randomNumber } from '../src/util/random';
import moment from 'moment';
/**
 * @description: 模拟网络请求延时
 * @param {*} time
 * @return {*}
 */
const waitTime = (time = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};
const hospitalNames = [
  '成都市第一人民医院',
  '四川大学华西医院',
  '四川省人民医院',
  '四川肿瘤医院',
  '四川大学华西口腔医院',
];
const address = [
  '四川省成都市高新区万象北路18号',
  '四川省成都市国学巷37号',
  '成都市青羊区一环路西2段32号',
  '成都市武侯区人民南路四段55号',
  '成都市武侯区人民南路三段14号',
];
const statusEnum = {
  0: 'Expire',
  1: 'Approving',
  2: 'Success',
  3: 'DueSoon',
};
// 医院管理信息表格数据
export let tableListDataSource = [
  {
    uid: randomID(),
    hospitalNames: '成都中医药大学附属医院',
    company: 'Telemedicine',
    address: '四川省成都市金牛区十二桥路39号',
    contract: `黄越创建的合同`,
    useMode: 'chapter',
    unusedMode: 'time',
    responsiblePerson: {
      name: '黄越',
      phone: '18384285043',
    },
    contractTime: [
      moment().subtract(Number(randomNumber(2)), 'days'),
      moment().add(Number(randomNumber(2)), 'days'),
    ],
    createTime: moment().subtract(Number(randomNumber(2)), 'days'),
    mangerName: randomName(),
    deviceList: [{ deviceName: 'GE' }, { deviceName: 'CT' }],
    // 该医院所拥有的账号
    bypassAccountList: [
      {
        name: 'user', // user 这个号属于这个医院（成都中医药大学附属医院）
        role: 'user',
      },
      {
        name: randomName(),
        role: 'user',
      },
      {
        name: randomName(),
        role: 'user',
      },
    ],
    // 该医院所拥有的工程师
    engineerList: [
      {
        name: randomName(),
        role: 'engineer',
      },
      {
        name: randomName(),
        role: 'engineer',
      },
      {
        name: randomName(),
        role: 'engineer',
      },
    ],
    status: 'Success',
  },
];
for (let i = 0; i < hospitalNames.length - 1; i += 1) {
  let tempName = randomName();
  tableListDataSource.push({
    uid: randomID(),
    hospitalNames: hospitalNames[i],
    company: 'Telemedicine',
    address: address[i],
    contract: `${tempName}创建的合同`,
    useMode: 'chapter',
    unusedMode: 'time',
    responsiblePerson: {
      name: tempName,
      phone: randomPhoneNumber(),
    },
    contractTime: [
      moment().subtract(Number(randomNumber(2)), 'days'),
      moment().add(Number(randomNumber(2)), 'days'),
    ],
    createTime: moment().subtract(Number(randomNumber(1)), 'days'),
    mangerName: randomName(),
    deviceList: [{ deviceName: 'GE' }, { deviceName: 'CT' }],
    bypassAccountList: [
      {
        name: randomName(),
        role: 'user',
      },
      {
        name: randomName(),
        role: 'user',
      },
      {
        name: randomName(),
        role: 'user',
      },
    ],
    engineerList: [
      {
        name: randomName(),
        role: 'engineer',
      },
      {
        name: randomName(),
        role: 'engineer',
      },
      {
        name: randomName(),
        role: 'engineer',
      },
    ],
    status: 'Success',
  });
}
export default {
  'GET /api/getAllHospital': async (req, res) => {
    await waitTime(2000);
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
  'POST /api/addHospital': async (req, res) => {
    const current = req.body;
    const tempHospital = {
      uid: current.uid,
      hospitalNames: current.hospitalNames,
      company: current.company,
      address: current.address,
      contract: current.contract,
      useMode: current.useMode,
      unusedMode: current.unusedMode,
      responsiblePerson: {
        name: current.responsiblePersonName,
        phone: current.responsiblePersonPhone,
      },
      contractTime: current.contractTime,
      createTime: moment(),
      mangerName: current.mangerName,
      deviceList: [],
      bypassAccountList: [],
      engineerList: [],
      status: 'Success',
    };
    tableListDataSource.unshift(tempHospital);
    await waitTime(2000);
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
  'POST /api/updateHospital': async (req, res) => {
    const current = req.body;
    const tempHospital = {
      uid: current.uid,
      hospitalNames: current.hospitalNames,
      company: current.company,
      address: current.address,
      contract: current.contract,
      useMode: current.useMode,
      unusedMode: current.unusedMode,
      responsiblePerson: {
        name: current.responsiblePersonName,
        phone: current.responsiblePersonPhone,
      },
      contractTime: current.contractTime,
      mangerName: current.mangerName,
    };
    const temp = [];
    tableListDataSource.forEach((item) => {
      if (item.uid === current.uid) {
        temp.push({ ...item, ...tempHospital });
      } else {
        temp.push(item);
      }
    });
    tableListDataSource = temp;
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
  'POST /api/deleteHospital': async (req, res) => {
    const uid = req.body;
    const temp = [];
    tableListDataSource.forEach((item) => {
      if (item.uid !== uid) {
        temp.push(item);
      }
    });
    tableListDataSource = temp;
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
};
